<template>
  <el-dialog
    :title="!houses.bid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="houses" :rules="dataRule" ref="houses" @keyup.enter.native="housesSubmit()" label-width="80px">
    <el-form-item label="经纪人名字" prop="username">
      <el-input v-model="houses.username" placeholder="经纪人名字"></el-input>
    </el-form-item>
    <el-form-item label="经纪人手机号" prop="phone">
      <el-input v-model="houses.phone" placeholder="经纪人手机号"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="avatar">
      <el-input v-model="houses.avatar" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="身份证号" prop="idcard">
      <el-input v-model="houses.idcard" placeholder="身份证号"></el-input>
    </el-form-item>
    <el-form-item label="新房数量" prop="newHouseNum">
      <el-input v-model="houses.newHouseNum" placeholder="新房数量"></el-input>
    </el-form-item>
    <el-form-item label="二手房数量" prop="secondHandHouseNum">
      <el-input v-model="houses.secondHandHouseNum" placeholder="二手房数量"></el-input>
    </el-form-item>
    <el-form-item label="租房数量" prop="rentHouseNum">
      <el-input v-model="houses.rentHouseNum" placeholder="租房数量"></el-input>
    </el-form-item>
    <el-form-item label="省" prop="province">
      <el-input v-model="houses.province" placeholder="省"></el-input>
    </el-form-item>
    <el-form-item label="市" prop="city">
      <el-input v-model="houses.city" placeholder="市"></el-input>
    </el-form-item>
    <el-form-item label="区" prop="region">
      <el-input v-model="houses.region" placeholder="区"></el-input>
    </el-form-item>
    <el-form-item label="状态（0离职，1在职）" prop="status">
      <el-input v-model="houses.status" placeholder="状态（0离职，1在职）"></el-input>
    </el-form-item>
    <el-form-item label="添加时间" prop="createTime">
      <el-input v-model="houses.createTime" placeholder="添加时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="houses.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="housesSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        houses: {
          bid: 0,
          username: '',
          phone: '',
          avatar: '',
          idcard: '',
          newHouseNum: '',
          secondHandHouseNum: '',
          rentHouseNum: '',
          province: '',
          city: '',
          region: '',
          status: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          username: [
            { required: true, message: '经纪人名字不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '经纪人手机号不能为空', trigger: 'blur' }
          ],
          avatar: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          idcard: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' }
          ],
          newHouseNum: [
            { required: true, message: '新房数量不能为空', trigger: 'blur' }
          ],
          secondHandHouseNum: [
            { required: true, message: '二手房数量不能为空', trigger: 'blur' }
          ],
          rentHouseNum: [
            { required: true, message: '租房数量不能为空', trigger: 'blur' }
          ],
          province: [
            { required: true, message: '省不能为空', trigger: 'blur' }
          ],
          city: [
            { required: true, message: '市不能为空', trigger: 'blur' }
          ],
          region: [
            { required: true, message: '区不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态（0离职，1在职）不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '添加时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.houses.bid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['houses'].resetFields()
          if (this.houses.bid) {
            this.$http({
              url: this.$http.adornUrl(`/broker/broker/info/${this.houses.bid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.houses.username = data.broker.username
                this.houses.phone = data.broker.phone
                this.houses.avatar = data.broker.avatar
                this.houses.idcard = data.broker.idcard
                this.houses.newHouseNum = data.broker.newHouseNum
                this.houses.secondHandHouseNum = data.broker.secondHandHouseNum
                this.houses.rentHouseNum = data.broker.rentHouseNum
                this.houses.province = data.broker.province
                this.houses.city = data.broker.city
                this.houses.region = data.broker.region
                this.houses.status = data.broker.status
                this.houses.createTime = data.broker.createTime
                this.houses.updateTime = data.broker.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      housesSubmit () {
        this.$refs['houses'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/broker/broker/${!this.houses.bid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'bid': this.houses.bid || undefined,
                'username': this.houses.username,
                'phone': this.houses.phone,
                'avatar': this.houses.avatar,
                'idcard': this.houses.idcard,
                'newHouseNum': this.houses.newHouseNum,
                'secondHandHouseNum': this.houses.secondHandHouseNum,
                'rentHouseNum': this.houses.rentHouseNum,
                'province': this.houses.province,
                'city': this.houses.city,
                'region': this.houses.region,
                'status': this.houses.status,
                'createTime': this.houses.createTime,
                'updateTime': this.houses.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
